﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class UserControls_Login : System.Web.UI.UserControl
{
    AccessData ac = new AccessData();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["CustomerName"]== null || Session["CustomerName"]== "")
            {
                EnablePan(true);
            }
            else if (Session["CustomerName"] != null || Session["CustomerName"] != "")
            {
                lblCustomerName.Text = "" + Session["CustomerName"];
                EnablePan(false);
            }
        }
    }
    protected void lnkLogout_Click(object sender, EventArgs e)
    {
        Session["CustomerID"] = "";
        Session["UserName"] = "";
        Session["Password"] = "";
        Session["CustomerName"] = "";
        EnablePan(true);
            
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        string UserName = txtUserName.Text;
        string Password = txtPassword.Text;
        if (CheckLog(UserName,Password))
        {
            Session["UserName"] = UserName;
            Session["Password"] = Password;
            lblCustomerName.Text = "" + Session["CustomerName"];
            EnablePan(false);
            lblStatusLog.Text = "";
        }
        else
        {
            lblStatusLog.Text = "UserName or Password invalid! ";
        }
        

    }
    public bool CheckLog(string username,string password)
    {
        SqlDataReader dr = ac.ExecuteReader("Select UserName,Password,CustomerName,CustomerID from Customers");
        while (dr.Read())
        {
            if (dr[0].ToString()== username && dr[1].ToString()==password)
            {
                Session["CustomerID"] = int.Parse(dr[3].ToString());
                Session["CustomerName"] = dr[2].ToString();
                return true;
            }
        }
        return false;
    }
    public void EnablePan(bool enable)
    {
        panLogin.Visible = enable;
        panLogout.Visible = !enable;
    }
    protected void likRegister_Click(object sender, EventArgs e)
    {
        Response.Redirect("Register.aspx");
    }
}
